//
//  ViewController.swift
//  01-P-Timer(简单计时器)
//
//  Created by FF on 16/12/28.
//  Copyright © 2016年 Cold Wind. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    
    @IBOutlet weak var reset: UIButton!
    @IBOutlet weak var timerLabel: UILabel!
    @IBOutlet weak var playBtn: UIButton!
    @IBOutlet weak var pauseBtn: UIButton!
    
    var count = 0.0
    private var timer: Timer?
    private var isPlay = false
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        timerLabel.adjustsFontSizeToFitWidth = true
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    
    
    @IBAction func play(_ sender: Any) {
        if isPlay {
           return
        }
        isPlay = true
        playBtn.isEnabled = false
        print("计算")
//        timer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true, block: { (t) in
//            self.count += 0.1
//            self.timerLabel.text = "\(self.count)"
//            self.timerLabel.text = String(format: "%.01f", self.count)
//        })
        
        timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(calculate), userInfo: nil, repeats: true)
//
//        Timer = NSTimer.scheduledTimerWithTimeInterval(0.1, target: self, selector: #selector(ViewController.UpdateTimer), userInfo: nil, repeats: true)
    }
    
    
    @objc private func calculate (){
        self.count += 0.1
//        self.timerLabel.text = "\(self.count)"
        self.timerLabel.text = String(format: "%.01f", self.count)
    }
    
    @IBAction func pause() {
        isPlay = false
        playBtn.isEnabled = true
        print("暂停")
        timer?.invalidate()
    }
    
    
    @IBAction func reset(_ sender: Any) {
        print("重置")
        timer?.invalidate()
        isPlay = false
        count = 0
        timerLabel.text = "\(count)"
        playBtn.isEnabled = true
        pauseBtn.isEnabled = true
        
    }
    
    

}

